const app = getApp()
Page({
  data: {
    statusList: [
      { status: 0, name: '营业中' },
      { status: 1, name: '暂停营业' }
    ],
    shopObj: {
      shopName: '',
      shopId: '',
      status: 0,
      address: '',
      notice: '',
      logo: '',
      giveMoney: '',
      giveFree: '',
      goodsTotal: 0
    },
    configState: false,
    configContent: '',
    dialogBodyStyle: {
      width: '70%',
      height: '250rpx'
    }
  },
  onLoad: function (options) {
    this.getShopDetailItem()
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.getShopDetailItem()
    setTimeout(() => {
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 100);
  },
  radioChange(e) {
    let val = e.detail.value
    this.setData({
      "shopObj.status": val
    })
  },
  close() {
    this.setData({
      configState: false
    })
  },
  formSubmit: function (e) {
    let subObj = e.detail.value
    if (!subObj.shopName) {
      this.setData({
        configState: true,
        configContent: '店铺名称不能为空'
      })
      return
    }
    if (!subObj.address) {
      this.setData({
        configState: true,
        configContent: '店铺地址不能为空'
      })
      return
    }
    app.Http.http({
      url: app.Api.ShopMotifyShopInfo,
      method: 'POST',
      params: {
        shopName: subObj.shopName,
        shopId: Number(subObj.shopId),
        status: Number(this.data.shopObj.status),
        address: subObj.address,
        notice: subObj.notice,
        logo: subObj.logo,
        giveMoney: subObj.giveMoney,
        giveFree: subObj.giveFree
      }
    }).then(data => {
      app.goBack({}, 2, 1)
      wx.showToast({
        title: '修改成功！',
        icon: 'success',
        duration: 1000,
        mask: !0,
      })

    }).catch(err => {
      console.log(err)
    })
  },
  getShopDetailItem() {//获取当前店铺信息
    app.Http.http({
      url: app.Api.ShopQueryByUserId,
      method: 'POST',
      params: {

      }
    }).then(data => {
      this.data.statusList.map((item) => {
        if (Number(item.status) === Number(data.status)) {
          item['checked'] = 'true'
        } else {

        }
      })
      this.setData({
        statusList: this.data.statusList,
        shopObj: {
          shopName: data.shopName,
          shopId: data.shopId,
          status: data.status,
          address: data.address,
          notice: data.notice,
          logo: data.logo,
          giveMoney: data.giveMoney,
          giveFree: data.giveFree,
          goodsTotal: data.goodsTotal
        }
      })

    }).catch(err => {
      console.log(err)
    })
  },
  chooseImg(e) {
    let _this = this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        app.Http.upload({
          url: app.Api.Upload,
          params: tempFilePaths[0],
          loading: true
        }).then(data => {
          _this.setData({
            "shopObj.logo": data.data
          })
        }).catch(err => {
          console.log(err)
        })
      },
    });

  }
})